package com.common.frame.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.common.frame.extension.dpToPx
import com.common.frame.utils.Res
import com.common.frame.utils.ScreenUtil

/**
 * 用于RecyclerView设置分割线
 */
class DividerItemDecoration : RecyclerView.ItemDecoration {
    private var mDivider: Drawable? = null
    private var mShowFirstDivider = false
    private var mShowLastDivider = false

    private var leftMargin: Int = 0
    private var rightMargin: Int = 0

    constructor(context: Context, divider: Drawable?, margin: Int) : this(context, null) {
        mDivider = divider
        leftMargin = margin.dpToPx()
        rightMargin = margin.dpToPx()
    }
    constructor(context: Context, color: Int, margin: Int) : this(context, null) {
        mDivider = GradientDrawable().apply {
            val parseColor = Res.getColor(color)
            setSize(ScreenUtil.getScreenWidth(context), ScreenUtil.dip2px(context,0.5f))
            setColor(parseColor)
        }
        leftMargin = margin.dpToPx()
        rightMargin = margin.dpToPx()
    }
    constructor(context: Context, attrs: AttributeSet?) {
        val a = context.obtainStyledAttributes(attrs, intArrayOf(android.R.attr.listDivider))
        mDivider ?: let {
            mDivider = a.getDrawable(0)
        }
        a.recycle()
    }

    constructor(
        context: Context,
        attrs: AttributeSet?,
        showFirstDivider: Boolean,
        showLastDivider: Boolean
    ) : this(context, attrs) {
        mShowFirstDivider = showFirstDivider
        mShowLastDivider = showLastDivider
    }

    constructor(context: Context, divider: Drawable) : this(context, null) {
        mDivider = divider
    }

    constructor(
        context: Context,
        divider: Drawable,
        showFirstDivider: Boolean,
        showLastDivider: Boolean
    ) : this(context, divider) {
        mShowFirstDivider = showFirstDivider
        mShowLastDivider = showLastDivider
    }

    fun setMargin(margin: Int): DividerItemDecoration {
        leftMargin = margin.dpToPx()
        rightMargin = margin.dpToPx()
        return this
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        if (mDivider == null) {
            return
        }
        if (parent.getChildPosition(view) < 1) {
            return
        }
        if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
            outRect.top = mDivider!!.intrinsicHeight
        } else {
            outRect.left = mDivider!!.intrinsicWidth
        }
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        if (mDivider == null) {
            super.onDrawOver(c, parent, state)
            return
        }

        // Initialization needed to avoid compiler warning
        var left = 0
        var right = 0
        var top = 0
        var bottom = 0
        val size: Int
        val orientation = getOrientation(parent)
        val childCount: Int = parent.childCount
        if (orientation == LinearLayoutManager.VERTICAL) {
            size = mDivider!!.intrinsicHeight
            left = parent.paddingLeft
            right = parent.width - parent.paddingRight
        } else { //horizontal
            size = mDivider!!.intrinsicWidth
            top = parent.paddingTop
            bottom = parent.height - parent.paddingBottom
        }
        for (i in (if (mShowFirstDivider) 0 else 1) until childCount) {
            val child: View = parent.getChildAt(i)
            val params: RecyclerView.LayoutParams = child.layoutParams as RecyclerView.LayoutParams
            if (orientation == LinearLayoutManager.VERTICAL) {
                top = child.top - params.topMargin
                bottom = top + size
            } else { //horizontal
                left = child.left - params.leftMargin
                right = left + size
            }
            mDivider!!.setBounds(left + leftMargin, top, right - rightMargin, bottom)
            mDivider!!.draw(c)
        }

        // show last divider
        if (mShowLastDivider && childCount > 0) {
            val child: View = parent.getChildAt(childCount - 1)
            val params: RecyclerView.LayoutParams = child.layoutParams as RecyclerView.LayoutParams
            if (orientation == LinearLayoutManager.VERTICAL) {
                top = child.bottom + params.bottomMargin
                bottom = top + size
            } else { // horizontal
                left = child.right + params.rightMargin
                right = left + size
            }
            mDivider!!.setBounds(left + leftMargin, top, right - rightMargin, bottom)
            mDivider!!.draw(c)
        }
    }

    private fun getOrientation(parent: RecyclerView): Int {
        return if (parent.layoutManager is LinearLayoutManager) {
            val layoutManager: LinearLayoutManager = parent.layoutManager as LinearLayoutManager
            layoutManager.orientation
        } else {
            throw IllegalStateException(
                "DividerItemDecoration can only be used with a LinearLayoutManager."
            )
        }
    }
}